package com.wm.blog.ws.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog.center.common.annotation.NeedLogin;
import com.wm.blog.center.common.exception.BlogException;
import com.wm.blog.center.common.utils.R;
import com.wm.blog.center.common.utils.login.*;
import com.wm.blog.center.entity.BlogEntity;
import com.wm.blog.center.service.BlogService;
import com.wm.blog.center.service.MemberService;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author: weiming
 * @Date: 2021/5/19 14:24
 * @Description:
 */
@RestController
@Api(tags = "Hello")
public class HelloController {

    @Autowired
    MemberService memberService;

    @Autowired
    BlogService blogService;

    @ApiOperation("say hello")
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "说的话", name = "text", required = true, paramType = "path"),
    })
    @ApiOperation("say text")
    @GetMapping("/say/{text}")
    @NeedLogin
    public String say(@PathVariable String text){
        throw new BlogException(text);
    }

    @ApiOperation("分页查询blog")
    @GetMapping("/page")
    public R pageBlog(){
        Page<BlogEntity> blogEntityPage = new Page<>();
        Page<BlogEntity> page = blogService.page(blogEntityPage, new QueryWrapper<BlogEntity>());
        return R.ok().setData(page);
    }


    /*========== sa-token 测试==========*/
    // 测试登录，浏览器访问： http://localhost:1002/doUserLogin?username=zhang&password=123456
    @ApiOperation("测试用户登录")
    @PostMapping("doUserLogin")
    public R doUserLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if("user".equals(username) && "123123".equals(password)) {
            StpUserUtil.setLoginId(username);
            String tokenValue = StpUserUtil.getTokenValue();
            return R.ok().put("token", tokenValue);
        }
        return R.error("登录失败");
    }

    // 查询登录状态，浏览器访问： http://localhost:1002/isUserLogin
    @ApiOperation("查询用户登录状态")
    @GetMapping("isUserLogin")
    public R isUserLogin(String username, String password) {

        if (StpUserUtil.isLogin()){
            String tokenValue = StpUserUtil.getTokenValue();
            return R.ok().put("token", tokenValue);
        } else {
            return R.error();
        }

    }

    @ApiOperation("测试管理员登录")
    @PostMapping("doAdminLogin")
    public R doAdminLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if("admin".equals(username) && "123123".equals(password)) {
            StpAdminUtil.setLoginId(username);
            String tokenValue = StpAdminUtil.getTokenValue();
            return R.ok().put("token", tokenValue);
        }
        return R.error("登录失败");
    }

    // 查询登录状态，浏览器访问： http://localhost:1002/isUserLogin
    @ApiOperation("查询管理员登录状态")
    @GetMapping("isAdminLogin")
    public R isAdminLogin(String username, String password) {

        if (StpAdminUtil.isLogin()){
            String tokenValue = StpAdminUtil.getTokenValue();
            return R.ok().put("token", tokenValue);
        } else {
            return R.error();
        }

    }

    // 查询登录状态，浏览器访问： http://localhost:1002/isUserLogin
    @ApiOperation("用户可以访问")
    @GetMapping("userAndAdminCan")
    @SaCheckLogin(key = StpUserUtil.KEY)
    public R userAndAdminCan() {

        return R.ok("用户能访问");

    }

    // 查询登录状态，浏览器访问： http://localhost:1002/isUserLogin
    @ApiOperation("管理员才能访问")
    @GetMapping("adminCan")
    @SaCheckLogin(key = StpAdminUtil.KEY)
    public R adminCan() {

        return R.ok("管理员才能访问");

    }
    /*========== sa-token 测试 end==========*/
    @ApiOperation("listByIds")
    @GetMapping("/listByIds")
    public R listTest(@ApiParam(value = "账本模板id", required = true) @RequestBody List<Long> ids) {
        return R.ok().setData(ids);
    }
}
